# This PowerShell script was released under the Ms-PL license
# http://www.opensource.org/licenses/ms-pl.html
# This script was originally intended for use with Microsoft Dynamics AX 2009
# and maintained and distributed as a project on CodePlex
# http://dynamicsaxbuild.codeplex.com


# Expecting FROMPATH, TOPATH
param([string] $FROMPATH = $(throw "From Path is Mandatory"), [string] $TOPATH = $(throw "To Path is Mandatory"))

if((Test-Path ($FROMPATH)) -and (Test-Path ($TOPATH)))
{
	# Get all *.ald files in the FROMPATH
	$files = Get-ChildItem -LiteralPath $FROMPATH -filter "*.ald"
	if ($files -ne $null)
	{
		foreach ($file in $files)
		{
		# Check the first 20 lines in the file
			$label = Get-Content -TotalCount 20 -Encoding Unicode -LiteralPath $file.FullName
			if ($label -ne $null)
			{
				ForEach ($line in $label)
				{
			# See if any line matches @XYZ### with actual text after the line
					if($line | Select-String -Pattern "@.{3}`\d+`\s.+")
					{
						"Copying language " + (($file.BaseName).SubString(5)) + " for label file " + (($file.BaseName).SubString(2,3))
			# If the file actually has text for labels, copy it
			# Make sure it is set to non-readonly at the same time.
						(Copy-Item -PassThru -Force -LiteralPath $file.FullName -Destination $TOPATH).IsReadOnly = $false
						Break
					}
				}
			}
		}
	}
}
